package com.ocom.exposeapi.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.HttpUtils;
import com.ocom.common.constant.ExposeApiUrl;
import com.ocom.common.entity.company.EntityComDockApi;
import com.ocom.common.enums.DockApiCodeEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.simple.ccbzhsh.CCBZHSHNewPersonInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.ccb.CcbzhshAddUserVo;
import com.ocom.common.vo.pyhandle.GetComWhiteListVo;
import com.ocom.exposeapi.feign.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;
@Slf4j
@RestController
public class CCBZHSHController {
    @Autowired
    private CompanyClientService companyClientService;
    @Autowired
    private PayHandleClientService payHandleClientService;

    @PostMapping(ExposeApiUrl.URL_ExposeApi_CCBZHSHNewPersonInfo)
    public Result ccbZHSHNewPersonInfo(@RequestBody CCBZHSHNewPersonInfoRequest request) {
        String dockApiCode = DockApiCodeEnum.ccbzhsh_newPersonInfo.getValue();
        Result apiParaResult = companyClientService.getApiPara(request.getComId(), dockApiCode);
        if (200 != apiParaResult.getCode()) {
            return ResultUtil.error(ResultEnum.Err_1000);
        }
        EntityComDockApi getApiPara = JSON.parseObject(JSON.toJSONString(apiParaResult.getData()), EntityComDockApi.class);
        StringBuilder apiPara = new StringBuilder();
        Integer personVer = 0;
        int limit = 100;
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(getApiPara.getApiPara())) {

            map = JSON.parseObject(getApiPara.getApiPara(), HashMap.class);
            if (!StringUtils.isNumeric(map.get("personVer").toString())) {
                map.put("personVer", "0");
            }
            personVer = Integer.valueOf(map.get("personVer").toString());

        } else {
            initApiPara(apiPara, "0");
        }

        if (request.getAutoSize() >0) {
            limit = request.getAutoSize();
        }

        try {
            Result personList = payHandleClientService.getComWhiteList(request.getComId(), personVer, limit);
            if (200 == personList.getCode()) {
                List<GetComWhiteListVo> getComWhiteListVos = JSON.parseArray(JSON.toJSONString(personList.getData()), GetComWhiteListVo.class);
                Optional<GetComWhiteListVo> getMaxId = getComWhiteListVos.stream().max(Comparator.comparing(GetComWhiteListVo::getId));
                if (getMaxId.isPresent()) {
                    Integer maxId = getMaxId.get().getId();
//                    GetComRequestRequest getComRequestRequest = new GetComRequestRequest();
//                    getComRequestRequest.setComId(request.getComId());
//                    getComRequestRequest.setDockCode("ccbzhsh");
//                    getComRequestRequest.setDockApiCode(dockApiCode);
//                    Result getComRequestResult = companyClientService.getComRequest(getComRequestRequest);
//                    if (200 == getComRequestResult.getCode()) {
//                        GetComRequestResponse getComRequestResponse = JSON.parseObject(JSON.toJSONString(getComRequestResult.getData()), GetComRequestResponse.class);
//                        Map<String, Object> reqMap = new HashMap<>();
//                        reqMap.put("comAppid", request.getComId());
//                        reqMap.put("whiteList", getComWhiteListVos);
//                        String json = HttpUtils.postsJson(getComRequestResponse.getRequestUrl(), reqMap);
//                        log.info(json);
//                        Result results = JSON.parseObject(json, Result.class);
//                    }
                    if(getComWhiteListVos == null || getComWhiteListVos.size() == 0){
                        log.info("获取单位更新人员为空, 无需同步用户信息.");
                        return ResultUtil.success();
                    }
                    Result results = null;
                    for(GetComWhiteListVo getComWhiteListVo : getComWhiteListVos){
                        CcbzhshAddUserVo userVo = new CcbzhshAddUserVo();
                        userVo.setComId(request.getComId());
                        userVo.setPId(getComWhiteListVo.getPId());
                        if(getComWhiteListVo.getStatus() == 0){
                            results = payHandleClientService.ccbzhshAddUser(userVo);
                        }else if(getComWhiteListVo.getStatus() == 1){
                            results = payHandleClientService.URL_ccbzhshDeleteUser(userVo);
                        }
                    }
                    if (200 == results.getCode()) {
                        map.put("personVer", maxId);
                        apiPara = new StringBuilder(JSON.toJSONString(map));
                        companyClientService.setApiPara(request.getComId(), DockApiCodeEnum.ccbzhsh_newPersonInfo.getValue(), apiPara.toString());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.success();
    }

    private void initApiPara(StringBuilder apiPara, String personVer) {
        apiPara.append("{");
        apiPara.append("\"personVer\"");
        apiPara.append(":");
        apiPara.append("\"");
        apiPara.append(personVer);
        apiPara.append("\"");
        apiPara.append("}");

    }

}
